package com.black.auth.authority;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * @author linwenfeng
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequiredArgsConstructor()
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Resource
    private final UserDetailsService userDetailsServiceImpl;

    @Resource
    private final UserDetailsService wxUserDetailsServiceImpl;


    @Value("${wx.miniapp.clinetId}")
    private String mimiAppClientId;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsServiceImpl);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        authenticationProvider.setHideUserNotFoundExceptions(false);
        return authenticationProvider;
    }

    @Override
    public void configure(WebSecurity web) {
        // 将 check_token 暴露出去，否则资源服务器访问时报 403 错误
        web.ignoring().antMatchers("/oauth/check_token");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 必须配置，不然OAuth2的http配置不生效----不明觉厉
                .requestMatchers()
                .antMatchers("/oauth/login", "/oauth/authorizes", "/oauth/authorize", "/oauth/clientCheck/*")
                .and()
                .authorizeRequests()
                // 自定义页面或处理url是，如果不配置全局允许，浏览器会提示服务器将页面转发多次
                .antMatchers("/oauth/login", "/oauth/authorizes", "/oauth/authenticate", "/oauth/clientCheck/*")
                .permitAll()
                .anyRequest()
                .authenticated();
        // 表单登录
        http.csrf().disable().httpBasic().disable().formLogin().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
    }

    /**
     * 不定义没有password grant_type
     *
     * @return AuthenticationManager
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() {
        ShareAuthenticationManager shareAuthenticationManager = new ShareAuthenticationManager(authenticationProvider());
        shareAuthenticationManager.putSpecialProvider(mimiAppClientId,new WechatAuthenticationProvider(wxUserDetailsServiceImpl));
        return shareAuthenticationManager;
    }

}
